package org.jeecg.modules.xxgx.djgl.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.innerApi.common.requestDto.InvoiceParam;
import org.jeecg.modules.xxgx.common.enums.KpztEnum;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdj;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjCezs;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjExt;
import org.jeecg.modules.xxgx.djgl.entity.CsXxDkdjmx;
import org.jeecg.modules.xxgx.djgl.entity.cezs.Cezs;
import org.jeecg.modules.xxgx.djgl.mapper.CsXxDkdjMapper;
import org.jeecg.modules.xxgx.djgl.mapper.CsXxDkdjmxMapper;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjCezsService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjExtService;
import org.jeecg.modules.xxgx.djgl.service.ICsXxDkdjService;
import org.jeecg.modules.xxgx.fjys.vo.FjysVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 待开单据
 * @Author: jeecg-boot
 * @Date:   2023-08-18
 * @Version: V1.0
 */
@Service
public class CsXxDkdjServiceImpl extends ServiceImpl<CsXxDkdjMapper, CsXxDkdj> implements ICsXxDkdjService {

	@Autowired
	private CsXxDkdjMapper csXxDkdjMapper;
	@Autowired
	private CsXxDkdjmxMapper csXxDkdjmxMapper;

    @Autowired
    private ICsXxDkdjExtService iCsXxDkdjExtService;

	@Autowired
	private ICsXxDkdjCezsService iCsXxDkdjCezsService;



	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(CsXxDkdj csXxDkdj, List<CsXxDkdjmx> csXxDkdjmxList) {
		csXxDkdjMapper.insert(csXxDkdj);
		if(csXxDkdjmxList!=null && csXxDkdjmxList.size()>0) {
			for(CsXxDkdjmx entity:csXxDkdjmxList) {
				//外键设置
				entity.setFromid(csXxDkdj.getId());
				csXxDkdjmxMapper.insert(entity);
			}
		}
		String company_id = csXxDkdj.getCompanyId();
		Integer tenant_id = csXxDkdj.getTenantId();
		//处理附加要素
		List<FjysVO> fjysList = csXxDkdj.getFjysList();
		if(!fjysList.isEmpty()){
			CsXxDkdjExt csXxDkdjExt = new CsXxDkdjExt();
			csXxDkdjExt.setId(csXxDkdj.getId());
			csXxDkdjExt.setFromid(csXxDkdj.getId());
			csXxDkdjExt.setCompanyId(company_id);
			csXxDkdjExt.setTenantId(tenant_id);
			csXxDkdjExt.setFjysInfo(JSONObject.toJSONString(fjysList));
			iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
		}

		//处理特定业务
		CsXxDkdjExt csXxDkdjExt = new CsXxDkdjExt();

		if (StringUtils.isNotBlank(csXxDkdj.getTdyslxdm())) {
			switch (csXxDkdj.getTdyslxdm()) {
				case "03":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywJzfw()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "04":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywHwysfwList()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "05":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywBdcxs()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "06":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywBdcjyzlfw()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "09":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywLkysfwList()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				default:
			}
		}

		//处理差额征收
		List<Cezs> cezsList = csXxDkdj.getCezsList();
		if (cezsList != null && !cezsList.isEmpty()) {
			for (Cezs cezs : cezsList) {
				CsXxDkdjCezs csXxDkdjCezs = new CsXxDkdjCezs();
				cezs.setId(IdWorker.getIdStr());
				cezs.setFromid(csXxDkdj.getId());
				cezs.setTenantId(tenant_id.toString());
				cezs.setSysOrgCode(csXxDkdj.getSysOrgCode());
				cezs.setCompanyId(company_id);
				BeanUtils.copyProperties(cezs, csXxDkdjCezs);
				iCsXxDkdjCezsService.save(csXxDkdjCezs);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(CsXxDkdj csXxDkdj,List<CsXxDkdjmx> csXxDkdjmxList) {
		csXxDkdjMapper.updateById(csXxDkdj);

		//1.先删除子表数据
		csXxDkdjmxMapper.deleteByMainId(csXxDkdj.getId());

		//2.子表数据重新插入
		if(csXxDkdjmxList!=null && csXxDkdjmxList.size()>0) {
			for(CsXxDkdjmx entity:csXxDkdjmxList) {
				//外键设置
				entity.setFromid(csXxDkdj.getId());
				csXxDkdjmxMapper.insert(entity);
			}
		}

		//处理特定业务
		//删除ext表
		QueryWrapper<CsXxDkdjExt> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("fromid",csXxDkdj.getId());
		iCsXxDkdjExtService.remove(queryWrapper);

		String company_id = csXxDkdj.getCompanyId();
		Integer tenant_id = csXxDkdj.getTenantId();
		//处理附加要素
		List<FjysVO> fjysList = csXxDkdj.getFjysList();
		if(fjysList != null && !fjysList.isEmpty()){
			CsXxDkdjExt csXxDkdjExt = new CsXxDkdjExt();
			csXxDkdjExt.setId(csXxDkdj.getId());
			csXxDkdjExt.setFromid(csXxDkdj.getId());
			csXxDkdjExt.setCompanyId(company_id);
			csXxDkdjExt.setTenantId(tenant_id);
			csXxDkdjExt.setFjysInfo(JSONObject.toJSONString(fjysList));
			iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
		}

		//处理特定业务
		CsXxDkdjExt csXxDkdjExt = new CsXxDkdjExt();

		if (StringUtils.isNotBlank(csXxDkdj.getTdyslxdm())) {
			switch (csXxDkdj.getTdyslxdm()) {
				case "03":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywJzfw()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "04":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywHwysfwList()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "05":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywBdcxs()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "06":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywBdcjyzlfw()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				case "09":
					csXxDkdjExt.setId(csXxDkdj.getId());
					csXxDkdjExt.setCompanyId(company_id);
					csXxDkdjExt.setTenantId(tenant_id);
					csXxDkdjExt.setFromid(csXxDkdj.getId());
					csXxDkdjExt.setTdywInfo(JSONObject.toJSONString(csXxDkdj.getTdywLkysfwList()));
					iCsXxDkdjExtService.saveOrUpdate(csXxDkdjExt);
					break;
				default:
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		csXxDkdjmxMapper.deleteByMainId(id);
		csXxDkdjMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			csXxDkdjmxMapper.deleteByMainId(id.toString());
			csXxDkdjMapper.deleteById(id);
		}
	}

	/**
	 * 更新待开单据开票状态
	 * @param invoiceParam
	 * @param result
	 */
	@Override
	public void updateDkdjKjzt(InvoiceParam invoiceParam, Result result) {
		CsXxDkdj updateDkdj = new CsXxDkdj();
		updateDkdj.setId(invoiceParam.getFpsqlsh());
		updateDkdj.setKpzt(KpztEnum.kpsb.getValue());
		updateDkdj.setSbyy(result.getMessage());
		this.updateById(updateDkdj);
	}

}
